package com.lp.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.lp.bo.IotHistorySensorDataBO;
import com.lp.bo.IotSensorInfoBO;
import com.lp.bo.historySimpleData;
import com.lp.cache.CacheName;
import com.lp.cache.ProCacheUtil;
import com.lp.common.Code;
import com.lp.service.IotHistorySensorDataService;
import com.lp.util.ObjectUtil;

@Service
public class IotHistorySensorDataServiceImpl extends BaseServiceImpl implements IotHistorySensorDataService {

	@Override
	public Map<String, Object> getHistorySensorData(IotHistorySensorDataBO obj) {
		Map<String, Object> resultMap = getResultMap();
		try{
			List<IotSensorInfoBO> list = new ArrayList<>();
			
			if(ObjectUtil.isNotEmpty(obj.getIds())){
				String[] ids = obj.getIds().split(",");
				Integer max = 0 ;
				for(String id : ids ){
					IotSensorInfoBO sensor = ProCacheUtil.getCache(CacheName.SENSORINFO, id);
					if(ObjectUtil.isNotEmpty(sensor)){
						obj.setSensor_id( Integer.parseInt(id) );
						List<historySimpleData> historyList = dao.selectList("IotHistorySensorData.selectSimple", obj);
						Integer count = dao.selectCount("IotHistorySensorData.selectPageCount", obj); 
						if( count/obj.getLimit()+1 > max ){
							max = count/obj.getLimit()+1 ;
						}
						sensor.setHistoryDara(historyList);
						list.add(sensor);						
					}
				}
				resultMap.put("totalPage", max);
				putData(resultMap, list) ;
			}else{
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR) ;
			}
		}catch(Exception e){
			exception(e, obj);
		}
		return resultMap;
	}
	
	
	
}
